﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SundayWishModel.Entities;
using SundayWishBLL;
public partial class User_privatedish_editprivatedish : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ContentPlaceHolder UserMaster = (ContentPlaceHolder)Page.Master.Master.FindControl("ContentPlaceHolder1");
            ((Label)UserMaster.FindControl("lbpagename")).Text = "修改个人食谱";
            if (Session["DishId"]!= null)
            {
                int DishId = Convert.ToInt32(Session["DishId"].ToString());
                PrivateDishHelper pdh = new PrivateDishHelper();
                Dish dish = pdh.GetDish(DishId);
                dish.DishStep = dish.DishStep.Replace("<br/>", "\r\n");
                dish.DishIntroduction = dish.DishIntroduction.Replace("<br/>", "\r\n");
                tbDishName.Text = dish.DishName;
                tbMaterial.Text = dish.DishMaterial;
                tbStep.Text = dish.DishStep;
                tbIntroduction.Text = dish.DishIntroduction;
                foreach (ListItem item in ddlKind.Items)
                {
                    if (item.Value == dish.DishKind)
                    {
                        item.Selected = true;
                        break;
                    }
                }
                ViewState["DishId"] = DishId;
                Session.Remove("DishId");
            }
            else
            {
                Response.Redirect("privatedishlist.aspx");
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        PrivateDishHelper pdh = new PrivateDishHelper();
        Dish dish = pdh.GetDish(Convert.ToInt32(ViewState["DishId"].ToString()));
        dish.DishName = tbDishName.Text;
        dish.DishMaterial = tbMaterial.Text;
        dish.DishStep = tbStep.Text;
        dish.DishIntroduction = tbIntroduction.Text;
        dish.LastModifyDate = DateTime.Now;
        dish.CreateDate = DateTime.Now;
        dish.DishKind = ddlKind.SelectedValue;
        dish.UserId = ((UserInfo)Session["user"]).UserId;
        dish.DishStep = dish.DishStep.Replace("\r\n", "<br/>");
        dish.DishIntroduction = dish.DishIntroduction.Replace("\r\n", "<br/>");
        pdh.UpdateDish(dish);
        Page.RegisterStartupScript("save", "<script>alert(\"保存成功\");this.location.href='" + dish.DishId.ToString() + "';</script>");
    }
}
